package com.lepu.app.fun.index.adapter;

import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.application.MyApplication;
import com.app.utils.ObserverConst;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.ObserverManager;
import com.core.lib.widget.MyDialog;
import com.lepu.app.fun.index.activity.IndexRemindAddActivity;
import com.lepu.app.fun.index.remind.Alarm;
import com.lepu.app.fun.index.remind.AlarmTipsManager;
import com.lepu.pasm.R;
public class IndexRemindAdapter extends BaseAdapter {
	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private ArrayList<Alarm> mArrayList = null;

	public IndexRemindAdapter(Context context,ArrayList<Alarm> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			
		    convertView = mLayoutInflater.inflate(R.layout.index_remind_item, null);

			holder = new ViewHolder();

			RelativeLayout itemLayout = (RelativeLayout) convertView.findViewById(R.id.itemLayout);
			TextView nameTextView = (TextView) convertView.findViewById(R.id.nameText);
			TextView usageTextView = (TextView) convertView.findViewById(R.id.usageText);
			TextView editTextView = (TextView) convertView.findViewById(R.id.editText);
			TextView deleteTextView = (TextView) convertView.findViewById(R.id.deleteText);
			LinearLayout timeLayout = (LinearLayout) convertView.findViewById(R.id.timeLayout);

			holder.itemLayout = itemLayout;
			holder.nameTextView = nameTextView;
			holder.usageTextView = usageTextView;
			holder.editTextView = editTextView;
			holder.deleteTextView = deleteTextView;
			holder.timeLayout = timeLayout;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final Alarm item = mArrayList.get(position);
		
		integrationTime(item, holder.timeLayout);
		
		holder.nameTextView.setText(item.yaoName);
		holder.usageTextView.setText(item.yaoType);
		holder.editTextView.setOnClickListener(new MyViewOnclicklistener(item));
		holder.deleteTextView.setOnClickListener(new MyViewOnclicklistener(item));
		holder.itemLayout.setOnClickListener(new MyViewOnclicklistener(item));
		
		// 长按监听
		holder.itemLayout.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				// 必须空实现，否则外部长按监听不起作用
				return false;
			}
		});
		
		return convertView;
	}
	
	 class MyViewOnclicklistener implements View.OnClickListener {
	        private Alarm alarm;

	        public MyViewOnclicklistener(Alarm alarm) {
	            this.alarm = alarm;
	        }
	        

	        @Override
	        public void onClick(View v) {
	            final int rid = v.getId();
	            switch (rid) {
	                case R.id.itemLayout: 
	                    
	                    break;
	                case R.id.deleteText:
	                	deleteRemind(alarm.yaoName);
	                	break;
	                case R.id.editText:
	                	Intent intent = new Intent(mContext,IndexRemindAddActivity.class);
	                    Bundle bundle = new Bundle();
	                    bundle.putSerializable("alarm", alarm);
	                    intent.putExtras(bundle);
	                    ((BaseFragmentActivity)mContext).startActivity(intent,true);
	                	break;
	                default:
	                    break;
	            }
	        }
	    }
	
	private void integrationTime(Alarm alarm, LinearLayout linearLayout) {
	    linearLayout.removeAllViews();
	    List<Alarm> alarmList = AlarmTipsManager.getAlarmByName(alarm.yaoName);
	        //过滤出时间
	    for (Alarm alarm1 : alarmList) {
	        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
	        layoutParams.setMargins(0, 0, 15, 0);
	        TextView textView = new TextView(mContext);
	        textView.setText(alarm1.time);
	        textView.setTextSize(13);
	        textView.setTextColor(mContext.getResources().getColor(R.color.color_666));
	        textView.setLayoutParams(layoutParams);
	        linearLayout.addView(textView);
	    }
	}
	
	public void deleteRemind(final String name) {
		
		String message = "您确定删除该提醒吗？";
		MyDialog dialog = new MyDialog((BaseFragmentActivity)mContext)
				.setMessage(message)
				.setNegativeButton(R.string.app_cancel,
						R.drawable.btn_cancel_selector, R.color.color_white,
						new View.OnClickListener() {

							@Override
							public void onClick(View v) {

							}
						})
				.setPositiveButton(R.string.app_ok,
						R.drawable.btn_ok_selector, R.color.color_white,
						new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								
								 List<Alarm> alarmList = AlarmTipsManager.getAlarmByName(name);
				                    for (Alarm alarm : alarmList) {
				                        //删除数据库数据
				                    	   Alarm.closeRemind(MyApplication.alarmManager, mContext, alarm.alarmId);
				                           AlarmTipsManager.deleteItem(alarm.alarmId); 
				                    }
								ObserverManager.getInstance().notify(ObserverConst.NOTIFY_REMIND_CHANGE, null,null);
								
							}
						});

		dialog.create(null);
		dialog.showMyDialog();
	}

	private static class ViewHolder {
		RelativeLayout itemLayout;
		TextView nameTextView;
		TextView usageTextView;
		TextView editTextView;
		TextView deleteTextView;
		LinearLayout timeLayout;
	}
}
